package com.imooc.utils.ffmpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FFMpegVideoImageUtils {


    /**
     * 去除音轨
     *
     * @param ffmpegExePath   ffmpeg路径
     * @param videoFilePath   视频输入路径
     * @param videoFilePath   视频输入路径
     * @param outputImagePath 视频输出路径
     */
    public String getImageFromVideo(String ffmpegExePath, String videoFilePath, String outputImagePath
    ) {
        //1.消除原视频的音轨
        List<String> command = new ArrayList<String>();
        command.add(ffmpegExePath);
        command.add("-ss");
        command.add("00:00:01");
        command.add("-y");
        command.add("-i");
        command.add(videoFilePath);
        command.add("-vframes");
        command.add("1");
        //  command.add("0.001");
//        command.add("-s");
//        command.add("320*240");
        command.add(outputImagePath);

        ProcessBuilder builder = new ProcessBuilder();

        builder.command(command);

        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        try {
            //开始执行命令
            Process process = builder.start();
            //如果你想获取到执行完后的信息，那么下面的代码也是需要的
            StringBuffer sbf = new StringBuffer();
            String line = null;
            InputStream inputStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(inputStreamReader);
            while ((line = br.readLine()) != null) {
                sbf.append(line);
                sbf.append(" ");
            }
            String resultInfo = sbf.toString();
            System.out.println(resultInfo);
            if (br != null) {
                br.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }

            //   convertor(ffmpegExePath, outputVideoPath ,audioFilePath ,duration , finalVideoPath );
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputImagePath;
    }


}
